package leetcode;

/**
 * leet code 2
 * You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
 * <p>
 * You may assume the two numbers do not contain any leading zero, except the number 0 itself.
 * <p>
 * Example:
 * <p>
 * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
 * Output: 7 -> 0 -> 8
 * Explanation: 342 + 465 = 807.
 */

import java.util.List;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }

    public void setNext(int x) {
        this.next = new ListNode(x);
    }

    public void outListNode() {
        StringBuffer sb = new StringBuffer();
        ListNode now = this;
        sb.append(now.val);
        while (now.next != null) {
            now = now.next;
            sb.append("  ->  ").append(now.val);
        }
        System.out.println(sb);
    }
}

public class AddTwoNumbers {

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(2);
        listNode1.setNext(4);
        listNode1.next.setNext(3);
        listNode1.outListNode();

        ListNode listNode2 = new ListNode(5);
        listNode2.setNext(6);
        listNode2.next.setNext(4);
        listNode2.outListNode();
        long start=System.currentTimeMillis();
        ListNode listNode3 = addTwoNumbers(listNode1, listNode2);
        long end=System.currentTimeMillis();
        listNode3.outListNode();
        System.out.println((end-start)+"ms");
    }
    //49 ms
    static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //init
        int val = l1.val + l2.val;
        int carry = val / 10;
        ListNode cacheNode = new ListNode(val % 10);
        //get sum list node
        while (l1.next != null || l2.next != null) {
            //judge null
            if (l1.next == null) {
                l1 = new ListNode(0);
            } else {
                l1 = l1.next;
            }
            if (l2.next == null) {
                l2 = new ListNode(0);
            } else {
                l2 = l2.next;
            }
            // calc
            val = l1.val + l2.val + carry;
            carry = val / 10;
            ListNode head = new ListNode(val % 10);
            head.next = cacheNode;
            cacheNode = head;
        }
        // in case of carry >0
        if (carry > 0) {
            ListNode head = new ListNode(carry);
            head.next = cacheNode;
            cacheNode = head;
        }
        // desc
        ListNode newNode = new ListNode(cacheNode.val);
        while (cacheNode.next != null) {
            ListNode head = new ListNode(cacheNode.next.val);
            head.next = newNode;
            newNode = head;
            cacheNode = cacheNode.next;
        }
        return newNode;
    }


}
